import 'package:flutter/material.dart';

import 'numeric_keyboard.dart';

typedef SubmitBTNCallBack = void Function(String prices);

class CalculatorView extends StatefulWidget {
  final SubmitBTNCallBack submitPrice;
  CalculatorView({Key key, this.submitPrice}) : super(key: key);

  @override
  _CalculatorView createState() => _CalculatorView();
}

class _CalculatorView extends State<CalculatorView> {
  var _text = "";
  var _count = "请输入金额";

  void initValue() {
    _text = "";
    _count = "请输入金额";
  }

  _onKeyboardTap(String value) {
    setState(() {
      if (value == "确定") {
        widget.submitPrice(_count);
        initValue();
        return;
      } else if (value == "+") {
        if (_text.isNotEmpty &&
            _text.substring(_text.length - 1, _text.length) == "+") {
          return;
        }
        _text += value;
        return;
      } else if (value == "-") {
        subText();
      } else {
        _text += value;
      }
      if (_count == "请输入金额") {
        _count = "0";
      }

      _count = calculaton().toString();
    });
  }

//循环计算计算器的输入的金额
  double calculaton() {
    double countPrice = 0;
    List<String> arr = _text.split("+");

    for (String item in arr) {
      if (item.isNotEmpty) {
        double _value = double.parse(item);
        countPrice += _value;
      }
    }
    return countPrice;
  }

//减法
  void subText() {
    if (_text.isEmpty) return;
    List<String> arr = _text.split("+");
    _text = "";
    if (arr[arr.length - 1].isEmpty) {
      arr.removeLast();
    }
    for (int i = 0; i < arr.length - 1; i++) {
      String item = arr[i];
      _text += item + "+";
    }
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      child: Column(
        children: [
          Container(
            height: 150,
            width: 450,
            padding: EdgeInsets.only(left: 20, bottom: 20),
            decoration: BoxDecoration(
              borderRadius: BorderRadius.circular(8),
              color: Color.fromRGBO(243, 243, 245, 1),
            ),
            child: Column(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Padding(
                  padding: const EdgeInsets.only(top: 15.0),
                  child: Text(
                    _text,
                    overflow: TextOverflow.ellipsis,
                    style: TextStyle(fontSize: 28, fontWeight: FontWeight.bold),
                  ),
                ),
                Text(
                  _count,
                  style: TextStyle(fontSize: 42, fontWeight: FontWeight.bold),
                ),
              ],
            ),
          ),
          NumericKeyboard(
              onKeyboardTap: _onKeyboardTap,
              textColor: Color.fromRGBO(72, 78, 99, 1),
              rightButtonFn: () {
                _onKeyboardTap("-");
              },
              rightIcon: Icon(
                Icons.backspace,
                color: Color.fromRGBO(72, 78, 99, 1),
              ),
              leftButtonFn: () {
                print('left button clicked');
              },
              leftIcon: Icon(
                Icons.check,
                color: Color.fromRGBO(72, 78, 99, 1),
              ),
              mainAxisAlignment: MainAxisAlignment.spaceEvenly)
        ],
      ),
    );
  }
}
